package com.itheima.controller;

import com.itheima.domain.Student;
import com.itheima.service.StudentService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class StudentController {
    @Reference
    private StudentService studentService;

    /*查询全部*/
    @RequestMapping("/list")
    public String findAll(Model model) {
        List<Student> all = studentService.findAll();
        model.addAttribute("list",all);
        return "list";
    }

    /*添加数据*/
    @RequestMapping("/toAddStudent")
    public String toSave() {
        return "add";
    }

    @RequestMapping("/addStudent")
    public String save(Student student) {
        studentService.save(student);
        return "redirect:/list";
    }

    /*修改数据*/
    @RequestMapping("/toUpdateStudent")
    public String toUpdate(Model model, Integer id) {
        Student students = studentService.findById(id);
        model.addAttribute("student", students);
        return "update";
    }

    @RequestMapping("/updateStudent")
    public String update(Model model, Student student) {
        studentService.update(student);
        Student students = studentService.findById(student.getId());
        model.addAttribute("student", student);
        return "redirect:/list";
    }

    /*删除*/
    @RequestMapping ("/deleteStudent/{id}")
    public String delete(@PathVariable("id") Integer id) {
        studentService.delete(id);
        return "redirect:/list";
    }
}
